package com.gogowan.agroenterprise.contorl.RetrofitUtil;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/6/1.
 */

public class JSON_Tool {
    private String RequseBody="";
    private String RequesHeader="";
    private int RequesCode=999;
    private String RequseMessage="";
    public static String GetJsonString(String jsonString, String name){
        JSONObject jsonObject=null;
        String str="";
        try {
           jsonObject=new JSONObject(jsonString);
            str=jsonObject.getString(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }
//    public static String GetJsonString_ImageView(String jsonString, String name){
//        JSONObject jsonObject=null;
//        String str="";
//        try {
//            jsonObject=new JSONObject(jsonString);
//            str= Utils.AppServers+jsonObject.getString(name);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return str;
//    }
    public static int GetJsonInteger(String jsonString, String name){
        JSONObject jsonObject=null;
        int str=0;
        try {
            jsonObject=new JSONObject(jsonString);
            str=jsonObject.getInt(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }
    public static double GetJsonDouble(String jsonString, String name){
        JSONObject jsonObject=null;
        double str=0.0;
        try {
            jsonObject=new JSONObject(jsonString);
            str=jsonObject.getDouble(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }
    public static Long GetJsonLong(String jsonString, String name){
        JSONObject jsonObject=null;
        long str=0;
        try {
            jsonObject=new JSONObject(jsonString);
            str=jsonObject.getLong(name);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static boolean GetJsonBoolean(String jsonString, String name){
        JSONObject jsonObject=null;
        boolean str=false;
        try {
            jsonObject=new JSONObject(jsonString);
            str=jsonObject.getBoolean(name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return str;
    }
    public static List<String> GetJsonArray(String jsonString){
        JSONArray array=null;
        List<String> list=new ArrayList<>();
        try {
            array=new JSONArray(jsonString);
            for(int i=0;i<array.length();i++){
                list.add(array.get(i).toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public JSON_Tool( ) {

    }
    public JSON_Tool(String requesJson) {
        setRequseBody(GetJsonString(requesJson,"respBody"));
        setRequesHeader(GetJsonString(requesJson,"respHeader"));
        setRequesCode(GetJsonInteger(getRequesHeader(),"resultCode"));
        setRequseMessage(GetJsonString(getRequesHeader(),"message"));
    }

    private void setRequseBody(String requseBody) {
        RequseBody = requseBody;
    }

    private void setRequesHeader(String requesHeader) {
        RequesHeader = requesHeader;
    }

    private void setRequesCode(int requesCode) {
        RequesCode = requesCode;
    }

    private void setRequseMessage(String requseMessage) {
        RequseMessage = requseMessage;
    }

    public String getRequseBody() {
        return RequseBody;
    }

    public String getRequesHeader() {
        return RequesHeader;
    }

    public int getRequesCode() {
        return RequesCode;
    }

    public String getRequseMessage() {
        return RequseMessage;
    }

    public static class jsonPutBuild {
        private JSONObject jsonObject = null;
        public JSONArray getJsonArray() {
            return jsonArray;
        }

        private JSONArray jsonArray=null;

        public jsonPutBuild() {
            jsonObject = new JSONObject();
            jsonArray  =new JSONArray();
        }
        public jsonPutBuild arrayAdd(Object value){
            jsonArray.put(value);
            return this;
        }
        public jsonPutBuild put(String name, JSONArray value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }
        public jsonPutBuild put(String name, String value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }

        public jsonPutBuild put(String name, int value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }
        public jsonPutBuild put(String name, double value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }
        public jsonPutBuild put(String name, JSONObject value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }

        public jsonPutBuild put(String name, boolean value) {
            try {
                jsonObject.put(name, value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }
        public jsonPutBuild put(Object o){
            try {
                jsonObject.put(o.getClass().getSimpleName(),GsonUtil.GsonString(o));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }
        public JSONObject getJson() {
            return jsonObject;
        }

        public JSONObject getReqBodyJson() {
            JSONObject jso = new JSONObject();
            try {
                jso.put("reqBody", jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jso;
        }
        public JSONObject getreqHeader(String Key, String Value){
            JSONObject jsonObject=new JSONObject();
            try {
                jsonObject.put(Key,Value);
                getReqBodyJson().put("reqHeader",jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return getReqBodyJson();
        }
        public RequestBody getRetrofitJson(){
            return RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        }
//        RequestBody.create(MediaType.parse("application/json"), build.getJson().toString());
        public RequestBody getRetrofitReqBodyJson(){
            return RequestBody.create(MediaType.parse("application/json"), getJson().toString());
        }
        public RequestBody getRetrofitReqBodyJson(String val){
            return RequestBody.create(MediaType.parse("application/json"), val);
        }
        public RequestBody getRetrofitReqHeaderJson(String Key, String Value){
            return RequestBody.create(MediaType.parse("application/json"), getreqHeader(Key,Value).toString());
        }
    }

}
